import 'package:flutter/material.dart';
import 'package:xvideos/hj_page/mine_business/welfare/widget/ad_list_sub_view.dart';
import 'package:xvideos/hj_utils/api_service/common_service.dart';
import 'package:xvideos/hj_utils/widget_utils.dart';
import 'package:xvideos/tools_base/loading/loading_center_widget.dart';

import '../../../hj_model/mine/happy/HappyModel.dart';
import '../../../hj_model/mine/happy/ShuApp.dart';
import '../../../tools_base/loading/l_error_widget.dart';

class LouFengAdPage extends StatefulWidget {
  final bool showAppbar;

  const LouFengAdPage({Key? key, this.showAppbar = false}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _LouFengAdPageState();
  }
}

class _LouFengAdPageState extends State<LouFengAdPage> with TickerProviderStateMixin {
  HappyModel? dataModel;
  bool isLoading = true;
  late final TabController tabController = TabController(length: tabs.length, vsync: this);
  final tabs = ["应用", "炮台", "棋牌", "直播", "游戏"];
  final dataSource = <ADRenderConfigModel>[];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      fetchPageData();
    });
  }

  void fetchPageData() async {
    dataModel = await CommonService.happyList();
    isLoading = false;
    if (dataModel == null) {
      setState(() {});
      return;
    }

    dataSource.add(ADRenderConfigModel(
      bannerAds: dataModel?.adv?.where((element) => element.moduleType == 1).toList(),
      hengAds: dataModel?.hengApp != null ? ShuAppRenderConfigModel(shuApps: dataModel?.hengApp, title: '官方推荐') : null,
      shuAds: dataModel?.shuApp != null ? ShuAppRenderConfigModel(shuApps: dataModel?.shuApp, title: '热门应用') : null,
    ));

    final hasYP = _hasTab(3, dataModel?.ypApp);
    if (!hasYP)
      tabs.remove('炮台');
    else
      dataSource.add(ADRenderConfigModel(
          bannerAds: dataModel?.adv?.where((element) => element.moduleType == 3).toList(),
          // hengAds: dataModel?.g,
          shuAds: dataModel?.ypApp != null ? ShuAppRenderConfigModel(shuApps: dataModel?.ypApp, title: '热门推荐') : null));
    final hasQP = _hasTab(4, dataModel?.qpApp);
    if (!hasQP)
      tabs.remove('棋牌');
    else
      dataSource.add(ADRenderConfigModel(
          bannerAds: dataModel?.adv?.where((element) => element.moduleType == 4).toList(),
          // hengAds: dataModel?.g,
          shuAds: dataModel?.qpApp != null ? ShuAppRenderConfigModel(shuApps: dataModel?.qpApp, title: '热门推荐') : null));
    final hasZB = _hasTab(5, dataModel?.zbApp);
    if (!hasZB)
      tabs.remove('直播');
    else
      dataSource.add(ADRenderConfigModel(
          bannerAds: dataModel?.adv?.where((element) => element.moduleType == 5).toList(),
          // hengAds: dataModel?.g,
          shuAds: dataModel?.zbApp != null ? ShuAppRenderConfigModel(shuApps: dataModel?.zbApp, title: '热门推荐') : null));
    final hasGame = _hasTab(2, dataModel?.gameApp);
    if (!hasGame)
      tabs.remove('游戏');
    else
      dataSource.add(ADRenderConfigModel(
        bannerAds: dataModel?.adv?.where((element) => element.moduleType == 2).toList(),
        shuAds: dataModel?.gameApp != null ? ShuAppRenderConfigModel(shuApps: dataModel?.gameApp, title: '热门推荐') : null,
      ));

    setState(() {});
  }

  bool _hasTab(int moduleType, List<ShuApp>? data) {
    var hasAd = false;
    dataModel?.adv?.forEach((element) {
      if (element.moduleType == moduleType) hasAd = true;
    });
    var hasData = data?.isNotEmpty ?? false;
    return hasAd || hasData;
  }

  @override
  Widget build(BuildContext context) {
    if (isLoading) return LoadingCenterWidget();
    if (dataModel == null)
      return CErrorWidget(
        retryOnTap: () {
          fetchPageData();
        },
      );

    return tabs.length > 1
        ? Column(
            children: [
              14.sizeBoxH,
              Container(
                margin: EdgeInsets.symmetric(horizontal: 9),
                child: TabBar(
                  padding: EdgeInsets.zero,
                  isScrollable: true,
                  tabAlignment: TabAlignment.start,
                  labelStyle: TextStyle(fontSize: 14, color: Color(0xE5FFFFFF)),
                  unselectedLabelStyle: TextStyle(fontSize: 14, color: Color(0xffACBABF)),
                  indicatorSize: TabBarIndicatorSize.tab,
                  indicatorColor: Color(0x00FFFFFF),
                  indicatorWeight: 1,
                  controller: tabController,
                  labelColor:  Color(0xE5FFFFFF),
                  labelPadding: EdgeInsets.zero,
                  tabs: tabs
                      .map((it) => Padding(
                            padding: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
                            child: Text(it),
                          ))
                      .toList(),
                ),
              ),
              Expanded(
                child: TabBarView(
                  controller: tabController,
                  children: dataSource.map((e) => ADListSubView(e)).toList(),
                ),
              ),
            ],
          )
        : ADListSubView(dataSource.first);
  }
}
